#include "spriteeditorbuttons.h"
#include "spriteeditor.h"
#include "imagebutton.h"

#define MACRO_SIMPLE_BUTTON_TOOL_BODY(TOOL_NAME, TOOL_TEXT_ID)	\
\
const char * TOOL_NAME::NAME = TOOL_TEXT_ID;	\
const char * TOOL_NAME::getName(){return NAME;}	\
\
void TOOL_NAME::execute()

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionNextFrame, "NextFrame")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	edit->getFrameViewer()->advanceFrame(+1);
}

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionPrevFrame, "PrevFrame")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	edit->getFrameViewer()->advanceFrame(-1);
}

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionDeleteClip, "DeleteClip")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	edit->getFrameViewer()->delSelectedClip();
}

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionAddClip, "AddClip")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	edit->getFrameViewer()->addSelectedClip();
}

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionMoveClipUp, "MoveClipUp")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	edit->getFrameViewer()->movSelectedClip(+1);
}

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionMoveClipDown, "MoveClipDown")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	edit->getFrameViewer()->movSelectedClip(-1);
}

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionFlipClip90, "FlipClip90")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	edit->getFrameViewer()->flipSelectedClip(VSDL_FLIP_90);
}

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionFlipClip180, "FlipClip180")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	edit->getFrameViewer()->flipSelectedClip(VSDL_FLIP_180);
}

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionFlipClipX, "FlipClipX")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	edit->getFrameViewer()->flipSelectedClip(VSDL_FLIP_X);
}

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionFlipClipY, "FlipClipY")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	edit->getFrameViewer()->flipSelectedClip(VSDL_FLIP_Y);
}

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionFlipClipXY, "FlipClipXY")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	edit->getFrameViewer()->flipSelectedClip(VSDL_FLIP_XY);
}

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionAddAnimation, "AddAnimation")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	edit->getAnimationViewer()->addAnimation();
}

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionAddFrame, "AddFrame")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	edit->getAnimationViewer()->addFrame();
}

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionAddCopyFrame, "AddCopyFrame")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	Sprite * s = edit->getSprite();
	int frameToCopy = s->getFrameIndex();
	SFrame * sfnew = edit->getAnimationViewer()->addFrame();
	sfnew->copy(*s->getFrame(frameToCopy));
}

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionRun, "RunAnimation")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	edit->setRunning(!edit->isRunning());

	ImageButton* ib = (ImageButton*)m_button;
	MasterPanel * mp = (MasterPanel *)m_button->getMasterPanel();
	if(edit->isRunning())
	{
		// set icon stop
		ib->setButtonImage(mp->getImageClip(8));
	}
	else
	{
		// set icon play
		ib->setButtonImage(mp->getImageClip(10));
	}
}

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionDeleteFrame, "DeleteFrame")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	edit->getFrameViewer()->deleteFrame();
}

MACRO_SIMPLE_BUTTON_TOOL_BODY(SpriteActionDeleteAnimation, "DeleteAnimation")
{
	SpriteEditor* edit = (SpriteEditor*)getContext();
	edit->getFrameViewer()->deleteAnimation();
	// after a frame or animation is deleted, the animation viewer must do some recalcualting...
	edit->getAnimationViewer()->hardRefresh();
	edit->getAnimationViewer()->refreshArea();
	edit->getAnimationViewer()->setDirty(0);
}

